﻿namespace Services.Implement;

public abstract partial class BaseBLL<T> where T : class, new()
{
    private readonly ISqlSugarClient _db;
    public BaseBLL(ISqlSugarClient db)
    {
        _db = db;
    }

    #region 插入数据
    /// <summary>
    /// 单个新增(同步)
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    public bool Insert(T entity)
    {
        return _db.Insertable(entity).ExecuteCommand() > 0;
    }
    /// <summary>
    /// 单个新增(同步)-泛型
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    public bool Insert<TEntity>(TEntity entity) where TEntity : class, new()
    {
        return _db.Insertable(entity).ExecuteCommand() > 0;
    }

    /// <summary>
    /// 单个新增(异步)
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    public async Task<bool> InsertAsync(T entity)
    {
        return await _db.Insertable(entity).ExecuteCommandAsync() > 0;
    }

    /// <summary>
    /// 单个新增(异步)-泛型
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    public async Task<bool> InsertAsync<TEntity>(TEntity entity) where TEntity : class, new()
    {
        return await _db.Insertable(entity).ExecuteCommandAsync() > 0;
    }
    /// <summary>
    /// 单个新增(同步)返回自增列
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    public int InsertReturnIdentity(T entity)
    {
        return _db.Insertable(entity).ExecuteReturnIdentity();
    }

    /// <summary>
    /// 单个新增(同步)返回自增列-泛型
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    public int InsertReturnIdentity<TEntity>(TEntity entity) where TEntity : class, new()
    {
        return _db.Insertable(entity).ExecuteReturnIdentity();
    }
    /// <summary>
    /// 单个新增(异步)返回自增列
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    public async Task<int> InsertReturnIdentityAsync(T entity)
    {
        return await _db.Insertable(entity).ExecuteReturnIdentityAsync();
    }
    /// <summary>
    /// 单个新增(异步)返回自增列-泛型
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    public async Task<int> InsertReturnIdentityAsync<TEntity>(TEntity entity) where TEntity : class, new()
    {
        return await _db.Insertable(entity).ExecuteReturnIdentityAsync();
    }
    /// <summary>
    /// 批量新增(同步)
    /// </summary>
    /// <param name="entitys"></param>
    /// <returns></returns>
    public bool Insert(List<T> entitys)
    {
        return _db.Insertable(entitys).ExecuteCommand() > 0;
    }

    /// <summary>
    /// 批量新增(同步)-泛型
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    public bool Insert<TEntity>(List<TEntity> entitys) where TEntity : class, new()
    {
        return _db.Insertable(entitys).ExecuteCommand() > 0;
    }
    /// <summary>
    /// 批量新增(异步)
    /// </summary>
    /// <param name="entitys"></param>
    /// <returns></returns>
    public async Task<bool> InsertAsync(List<T> entitys)
    {
        return await _db.Insertable(entitys).ExecuteCommandAsync() > 0;
    }

    /// <summary>
    /// 批量新增(异步)-泛型
    /// </summary>
    /// <param name="entitys"></param>
    /// <returns></returns>
    public async Task<bool> InsertAsync<TEntity>(List<TEntity> entitys) where TEntity : class, new()
    {
        return await _db.Insertable(entitys).ExecuteCommandAsync() > 0;
    }
    /// <summary>
    /// 批量新增(异步)1000条以上
    /// </summary>
    /// <param name="entitys"></param>
    /// <returns></returns>
    public async Task<bool> BulkInsertCopyAsync(List<T> entitys)
    {
        return await _db.Fastest<T>().BulkCopyAsync(entitys) > 0;
    }

    /// <summary>
    /// 批量新增(异步)1000条以上-泛型
    /// </summary>
    /// <param name="entitys"></param>
    /// <returns></returns>
    public async Task<bool> BulkInsertCopyAsync<TEntity>(List<TEntity> entitys) where TEntity : class, new()
    {
        return await _db.Fastest<TEntity>().BulkCopyAsync(entitys) > 0;
    }
    #endregion

    #region 更新数据
    /// <summary>
    /// 单个修改(同步)
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    public bool Update(T entity)
    {
        if (entity == null)
        {
            return false;
        }
        return _db.Updateable(entity).ExecuteCommand() > 0;
    }

    /// <summary>
    /// 单个修改(同步)-泛型
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    public bool Update<TEntity>(TEntity entity) where TEntity : class, new()
    {
        if (entity == null)
        {
            return false;
        }
        return _db.Updateable(entity).ExecuteCommand() > 0;
    }
    /// <summary>
    /// 单个修改(异步)
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    public async Task<bool> UpdateAsync(T entity)
    {
        if (entity == null)
        {
            return false;
        }
        return await _db.Updateable(entity).ExecuteCommandAsync() > 0;
    }

    /// <summary>
    /// 单个修改(异步)-泛型
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    public async Task<bool> UpdateAsync<TEntity>(TEntity entity) where TEntity : class, new()
    {
        if (entity == null)
        {
            return false;
        }
        return await _db.Updateable(entity).ExecuteCommandAsync() > 0;
    }
    /// <summary>
    /// 批量修改(同步)
    /// </summary>
    /// <param name="entitys"></param>
    /// <returns></returns>
    public bool Update(List<T> entitys)
    {
        if (entitys == null || entitys.Count == 0)
        {
            return false;
        }
        return _db.Updateable(entitys).ExecuteCommand() > 0;
    }

    /// <summary>
    /// 批量修改(同步)-泛型
    /// </summary>
    /// <param name="entitys"></param>
    /// <returns></returns>
    public bool Update<TEntity>(List<TEntity> entitys) where TEntity : class, new()
    {
        if (entitys == null || entitys.Count == 0)
        {
            return false;
        }
        return _db.Updateable(entitys).ExecuteCommand() > 0;
    }
    /// <summary>
    /// 批量修改(异步)
    /// </summary>
    /// <param name="entitys"></param>
    /// <returns></returns>
    public async Task<bool> UpdateAsync(List<T> entitys)
    {
        if (entitys == null || entitys.Count == 0)
        {
            return false;
        }
        return await _db.Updateable(entitys).ExecuteCommandAsync() > 0;
    }

    /// <summary>
    /// 批量修改(异步)-泛型
    /// </summary>
    /// <param name="entitys"></param>
    /// <returns></returns>
    public async Task<bool> UpdateAsync<TEntity>(List<TEntity> entitys) where TEntity : class, new()
    {
        if (entitys == null || entitys.Count == 0)
        {
            return false;
        }
        return await _db.Updateable(entitys).ExecuteCommandAsync() > 0;
    }
    /// <summary>
    /// 批量修改(异步)1000条以上
    /// </summary>
    /// <param name="entitys"></param>
    /// <returns></returns>
    public async Task<bool> BulkUpdateCopyAsync(List<T> entitys)
    {
        if (entitys == null || entitys.Count == 0)
        {
            return false;
        }
        return await _db.Fastest<T>().BulkUpdateAsync(entitys) > 0;
    }
    /// <summary>
    /// 批量修改(异步)1000条以上-泛型
    /// </summary>
    /// <param name="entitys"></param>
    /// <returns></returns>
    public async Task<bool> BulkUpdateCopyAsync<TEntity>(List<TEntity> entitys) where TEntity : class, new()
    {
        if (entitys == null || entitys.Count == 0)
        {
            return false;
        }
        return await _db.Fastest<TEntity>().BulkUpdateAsync(entitys) > 0;
    }
    #endregion

    #region 删除
    /// <summary>
    /// 单个删除(同步)
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    public bool Delete(T entity)
    {
        if (entity == null)
        {
            return false;
        }
        return _db.Deleteable(entity).ExecuteCommand() > 0;
    }

    /// <summary>
    /// 单个删除(同步)-泛型
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    public bool Delete<TEntity>(TEntity entity) where TEntity : class, new()
    {
        if (entity == null)
        {
            return false;
        }
        return _db.Deleteable(entity).ExecuteCommand() > 0;
    }
    /// <summary>
    /// 单个删除(异步)
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    public async Task<bool> DeleteAsync(T entity)
    {
        if (entity == null)
        {
            return false;
        }
        return await _db.Deleteable(entity).ExecuteCommandAsync() > 0;
    }

    /// <summary>
    /// 单个删除(异步)-泛型
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    public async Task<bool> DeleteAsync<TEntity>(TEntity entity) where TEntity : class, new()
    {
        if (entity == null)
        {
            return false;
        }
        return await _db.Deleteable(entity).ExecuteCommandAsync() > 0;
    }
    /// <summary>
    /// 根据id单个删除(同步)
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    public bool Delete(int id)
    {
        return _db.Deleteable<T>(id).ExecuteCommand() > 0;
    }
    /// <summary>
    /// 根据id单个删除(同步)-泛型
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public bool Delete<TEntity>(int id) where TEntity : class, new()
    {
        return _db.Deleteable<T>(id).ExecuteCommand() > 0;
    }

    /// <summary>
    /// 根据id单个删除(异步)
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public async Task<bool> DeleteAsync(int id)
    {
        return await _db.Deleteable<T>(id).ExecuteCommandAsync() > 0;
    }

    /// <summary>
    /// 根据id单个删除(异步)-泛型
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public async Task<bool> DeleteAsync<TEntity>(int id) where TEntity : class, new()
    {
        return await _db.Deleteable<TEntity>(id).ExecuteCommandAsync() > 0;
    }

    /// <summary>
    /// 根据委托表达式单个删除(同步)
    /// </summary>
    /// <param name="expression"></param>
    /// <returns></returns>
    public bool Delete(Expression<Func<T, bool>> expression)
    {
        return _db.Deleteable(expression).ExecuteCommand() > 0;
    }

    /// <summary>
    /// 根据委托表达式单个删除(同步)-泛型
    /// </summary>
    /// <param name="expression"></param>
    /// <returns></returns>
    public bool Delete<TEntity>(Expression<Func<TEntity, bool>> expression) where TEntity : class, new()
    {
        return _db.Deleteable(expression).ExecuteCommand() > 0;
    }

    /// <summary>
    /// 根据委托表达式单个删除(异步)
    /// </summary>
    /// <param name="expression"></param>
    /// <returns></returns>
    public async Task<bool> DeleteAsync(Expression<Func<T, bool>> expression)
    {
        return await _db.Deleteable(expression).ExecuteCommandAsync() > 0;
    }

    /// <summary>
    /// 根据委托表达式单个删除(异步)-泛型
    /// </summary>
    /// <param name="expression"></param>
    /// <returns></returns>
    public async Task<bool> DeleteAsync<TEntity>(Expression<Func<TEntity, bool>> expression) where TEntity : class, new()
    {
        return await _db.Deleteable(expression).ExecuteCommandAsync() > 0;
    }

    /// <summary>
    /// 批量删除(同步)
    /// </summary>
    /// <param name="entitys"></param>
    /// <returns></returns>
    public bool Delete(List<T> entitys)
    {
        if (entitys == null || entitys.Count == 0)
        {
            return false;
        }
        return _db.Deleteable(entitys).ExecuteCommand() > 0;
    }

    /// <summary>
    /// 批量删除(同步)-泛型
    /// </summary>
    /// <param name="entitys"></param>
    /// <returns></returns>
    public bool Delete<TEntity>(List<TEntity> entitys) where TEntity : class, new()
    {
        if (entitys == null || entitys.Count == 0)
        {
            return false;
        }
        return _db.Deleteable(entitys).ExecuteCommand() > 0;
    }

    /// <summary>
    /// 批量删除(异步)
    /// </summary>
    /// <param name="entitys"></param>
    /// <returns></returns>
    public async Task<bool> DeleteAsync(List<T> entitys)
    {
        if (entitys == null || entitys.Count == 0)
        {
            return false;
        }
        return await _db.Deleteable(entitys).ExecuteCommandAsync() > 0;
    }

    /// <summary>
    /// 批量删除(异步)-泛型
    /// </summary>
    /// <param name="entitys"></param>
    /// <returns></returns>
    public async Task<bool> DeleteAsync<TEntity>(List<TEntity> entitys) where TEntity : class, new()
    {
        if (entitys == null || entitys.Count == 0)
        {
            return false;
        }
        return await _db.Deleteable(entitys).ExecuteCommandAsync() > 0;
    }
    #endregion

    #region 查询
    /// <summary>
    /// 获取数量(同步)
    /// </summary>
    /// <param name="expression">expression</param>
    /// <returns>返回值</returns>
    public int GetCount(Expression<Func<T, bool>>? expression = null)
    {
        return _db.Queryable<T>()
            .WhereIF(expression != null, expression)
            .Count();
    }

    /// <summary>
    /// 获取数量(同步)-泛型
    /// </summary>
    /// <param name="expression">expression</param>
    /// <returns>返回值</returns>
    public int GetCount<TEntity>(Expression<Func<TEntity, bool>>? expression = null) where TEntity : class, new()
    {
        return _db.Queryable<TEntity>()
            .WhereIF(expression != null, expression)
            .Count();
    }

    /// <summary>
    /// 获取数量(异步)
    /// </summary>
    /// <param name="expression">expression</param>
    /// <returns>返回值</returns>
    public async Task<int> GetCountAsync(Expression<Func<T, bool>>? expression = null)
    {
        return await _db.Queryable<T>()
            .WhereIF(expression != null, expression)
            .CountAsync();
    }

    /// <summary>
    /// 获取数量(异步)-泛型
    /// </summary>
    /// <param name="expression">expression</param>
    /// <returns>返回值</returns>
    public async Task<int> GetCountAsync<TEntity>(Expression<Func<TEntity, bool>>? expression = null) where TEntity : class, new()
    {
        return await _db.Queryable<TEntity>()
            .WhereIF(expression != null, expression)
            .CountAsync();
    }

    /// <summary>
    /// 通过主键查询(同步)
    /// </summary>
    /// <param name="objId"></param>
    /// <returns></returns>
    public T Get(object objId)
    {
        return _db.Queryable<T>().InSingle(objId);
    }

    /// <summary>
    /// 通过主键查询(同步)-泛型
    /// </summary>
    /// <param name="objId"></param>
    /// <returns></returns>
    public TEntity Get<TEntity>(object objId) where TEntity : class, new()
    {
        return _db.Queryable<TEntity>().InSingle(objId);
    }

    /// <summary>
    /// 通过主键查询(异步)
    /// </summary>
    /// <param name="objId"></param>
    /// <returns></returns>
    public async Task<T> GetAsync(object objId)
    {
        return await _db.Queryable<T>().InSingleAsync(objId);
    }

    /// <summary>
    /// 通过主键查询(异步)-泛型
    /// </summary>
    /// <param name="objId"></param>
    /// <returns></returns>
    public async Task<TEntity> GetAsync<TEntity>(object objId) where TEntity : class, new()
    {
        return await _db.Queryable<TEntity>().InSingleAsync(objId);
    }

    /// <summary>
    /// 根据条件查询单个实体(同步)
    /// </summary>
    /// <param name="whereExpression"></param>
    /// <returns></returns>
    public T Get(Expression<Func<T, bool>> whereExpression)
    {
        return _db.Queryable<T>().First(whereExpression);
    }

    /// <summary>
    /// 根据条件查询单个实体(同步)-泛型
    /// </summary>
    /// <param name="whereExpression"></param>
    /// <returns></returns>
    public TEntity Get<TEntity>(Expression<Func<TEntity, bool>> whereExpression) where TEntity : class, new()
    {
        return _db.Queryable<TEntity>().First(whereExpression);
    }

    /// <summary>
    /// 根据条件查询单个实体(异步)
    /// </summary>
    /// <param name="whereExpression"></param>
    /// <returns></returns>
    public async Task<T> GetAsync(Expression<Func<T, bool>> whereExpression)
    {
        return await _db.Queryable<T>().FirstAsync(whereExpression);
    }
    /// <summary>
    /// 根据条件查询单个实体(异步)-泛型
    /// </summary>
    /// <param name="whereExpression"></param>
    /// <returns></returns>
    public async Task<TEntity> GetAsync<TEntity>(Expression<Func<TEntity, bool>> whereExpression) where TEntity : class, new()
    {
        return await _db.Queryable<TEntity>().FirstAsync(whereExpression);
    }

    /// <summary>
    /// 根据条件查询集合(同步)
    /// </summary>
    /// <param name="whereExpression"></param>
    /// <returns></returns>
    public List<T> GetList(Expression<Func<T, bool>>? whereExpression = null)
    {
        return _db.Queryable<T>().WhereIF(whereExpression != null, whereExpression).ToList();
    }

    /// <summary>
    /// 根据条件查询集合(同步)-泛型
    /// </summary>
    /// <param name="whereExpression"></param>
    /// <returns></returns>
    public List<TEntity> GetList<TEntity>(Expression<Func<TEntity, bool>>? whereExpression = null) where TEntity : class, new()
    {
        return _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).ToList();
    }

    /// <summary>
    /// 根据条件查询集合(异步)
    /// </summary>
    /// <param name="whereExpression"></param>
    /// <returns></returns>
    public async Task<List<T>> GetListAsync(Expression<Func<T, bool>>? whereExpression = null)
    {
        return await _db.Queryable<T>().WhereIF(whereExpression != null, whereExpression).ToListAsync();
    }
    /// <summary>
    /// 根据条件查询集合(异步)-泛型
    /// </summary>
    /// <param name="whereExpression"></param>
    /// <returns></returns>
    public async Task<List<TEntity>> GetListAsync<TEntity>(Expression<Func<TEntity, bool>>? whereExpression = null) where TEntity : class, new()
    {
        return await _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).ToListAsync();
    }

    /// <summary>
    /// 分页查询(同步)
    /// </summary>
    /// <param name="whereExpression">条件表达式</param>
    /// <param name="intPageIndex">页码（下标0）</param>
    /// <param name="intPageSize">页大小</param>
    /// <param name="intTotalCount">数据总量</param>
    /// <param name="strOrderByFileds">排序字段，如name asc,age desc</param>
    /// <returns>数据列表</returns>
    public List<T> PageList(
        Expression<Func<T, bool>> whereExpression,
        int intPageIndex,
        int intPageSize,
        ref int intTotalCount,
        string strOrderByFileds)
    {
        return _db.Queryable<T>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).ToPageList(intPageIndex, intPageSize, ref intTotalCount);
    }

    /// <summary>
    /// 分页查询(同步)-泛型
    /// </summary>
    /// <param name="whereExpression">条件表达式</param>
    /// <param name="intPageIndex">页码（下标0）</param>
    /// <param name="intPageSize">页大小</param>
    /// <param name="intTotalCount">数据总量</param>
    /// <param name="strOrderByFileds">排序字段，如name asc,age desc</param>
    /// <returns>数据列表</returns>
    public List<TEntity> PageList<TEntity>(
        Expression<Func<TEntity, bool>> whereExpression,
        int intPageIndex,
        int intPageSize,
        ref int intTotalCount,
        string strOrderByFileds) where TEntity : class, new()
    {
        return _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).ToPageList(intPageIndex, intPageSize, ref intTotalCount);
    }

    /// <summary>
    /// 分页查询(异步)
    /// </summary>
    /// <param name="whereExpression">条件表达式</param>
    /// <param name="intPageIndex">页码（下标0）</param>
    /// <param name="intPageSize">页大小</param>
    /// <param name="intTotalCount">数据总量</param>
    /// <param name="strOrderByFileds">排序字段，如name asc,age desc</param>
    /// <returns>数据列表</returns>
    public async Task<List<T>> PageListAsync(
        Expression<Func<T, bool>> whereExpression,
        int intPageIndex,
        int intPageSize,
        RefAsync<int> intTotalCount,
        string strOrderByFileds)
    {
        return await _db.Queryable<T>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).ToPageListAsync(intPageIndex, intPageSize, intTotalCount);
    }

    /// <summary>
    /// 分页查询(异步)-泛型
    /// </summary>
    /// <param name="whereExpression">条件表达式</param>
    /// <param name="intPageIndex">页码（下标0）</param>
    /// <param name="intPageSize">页大小</param>
    /// <param name="intTotalCount">数据总量</param>
    /// <param name="strOrderByFileds">排序字段，如name asc,age desc</param>
    /// <returns>数据列表</returns>
    public async Task<List<TEntity>> PageListAsync<TEntity>(
        Expression<Func<TEntity, bool>> whereExpression,
        int intPageIndex,
        int intPageSize,
        RefAsync<int> intTotalCount,
        string strOrderByFileds) where TEntity : class, new()
    {
        return await _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).ToPageListAsync(intPageIndex, intPageSize, intTotalCount);
    }
    #endregion

    public DbResult<bool> UseTran(Action action)
    {
        try
        {
            var result = _db.Ado.UseTran(() => action());
            return result;
        }
        catch (Exception ex)
        {
            _db.Ado.RollbackTran();
            throw;
        }
    }
}
